<template>
  <div>
    <Row :gutter="10">
      <Col>
      <Card>
        <div class="live_wrapper clearfix"
             style="margin-top: 10px">
          <Input v-model="filter.name"
                 @on-search="search"
                 search
                 enter-button="搜索"
                 placeholder="请输入商品名称"
                 style="width: 200px; margin-right: 10px; margin-bottom:10px; float:left;" />
          <Select v-model="filter.cate_id"
                  style="width: 140px; margin-right: 10px; margin-bottom:10px; float:left;"
                  filterable
                  @on-change="search"
                  placeholder="商品类别">
            <Option value=0>全部</Option>
            <Option :value="item.id"
                    v-for="(item, index) in cate_list"
                    :key="index">{{item.name}}</Option>
          </Select>
          <Select v-model="filter.status"
                  style="width: 130px; margin-right: 10px; margin-bottom:10px; float:left;"
                  filterable
                  @on-change="search"
                  placeholder="商品状态">
            <Option value=-1>全部</Option>
            <Option value="1">上架</Option>
            <Option value="0">下架</Option>
          </Select>
          <DatePicker type="daterange"
                      v-model="created_at"
                      @on-change="search"
                      placement="bottom-end"
                      placeholder="选择创建时间"
                      style="width: 200px; margin-right: 10px; margin-bottom:10px; float:left;"></DatePicker>
          <Button type="primary"
                  @click.stop="addGood()"
                  style="width: 80px;height: 34px;font-size: 14px; margin-right: 10px; margin-bottom:10px; float:left;">添加</Button>
          <Button type="primary"
                  @click.stop="exportData()"
                  style="width: 80px;height: 34px;font-size: 14px; margin-bottom:10px; float:left;">导出</Button>
        </div>
        <p class="admin_title">
          全部商品
        </p>
        <Row class="margin-top-10 table_pattern table_pattern2">
          <Table style="width: calc(100vw - 256px - 36px - 32px)"
                 :loading="loading"
                 :columns="columns"
                 :data="list"></Table>
        </Row>
        <Row class="margin-top-10">
          <Page :total="pagenation.totalPage"
                :page-size="pagenation.perPage"
                :current="pagenation.currentPage"
                show-elevator
                show-total
                @on-change="change"></Page>
        </Row>
      </Card>
      </Col>
    </Row>
  </div>
</template>
<script>
import store from '@/store'
import config from '@/config'
import Time from '@/tool/Time'
import { goodList, changeGoodStatus, goodCateForSelect } from '@/api/main'
export default {
  data () {
    return {
      pagenation: {},
      columns: [
        {
          key: 'name',
          title: '商品名称',
          align: 'center',
          render: (h, params) => {
            return h('strong', params.row.name)
          },
        },
        {
          key: 'cate_name',
          title: '商品类别',
          align: 'center',
          render: (h, params) => {
            return h('strong', params.row.cate_name)
          },
        },
        {
          key: 'img',
          title: '商品图封面',
          align: 'center',
          render: (h, params) => {
            return h(
              'div',
              {
                style: {
                  backgroundImage: 'url(' + params.row.img + ')',
                  width: '100px',
                  height: '100px',
                  objectFit: 'cover',
                  margin: '10px auto',
                  position: 'relative',
                  cursor: 'pointer',
                  backgroundSize: 'cover',
                }
              }
            )
          },
        },
        {
          key: 'price',
          title: '商品价格',
          align: 'center',
          render: (h, params) => {
            return h('strong', params.row.price)
          },
        },
        {
          key: 'store',
          title: '商品库存',
          align: 'center',
          render: (h, params) => {
            return h('strong', params.row.store)
          },
        },
        {
          key: 'desc',
          title: '商品简介',
          align: 'center',
          render: (h, params) => {
            let desc_show = ''
            if (params.row.desc) {
              desc_show = params.row.desc.length > 20 ? (params.row.desc.slice(0, 16) + '...') : params.row.desc
            }
            return h('Tooltip', {
              props: {
                content: params.row.desc,
                placement: "left-start",
                'max-width': '500',
                'transfer': true
              }
            }, desc_show)
          }
        },
        {
          key: 'status',
          title: '状态',
          align: 'center',
          render: (h, params) => h('i-switch', {
            props: {
              value: params.row.status,
              size: 'large',
              'true-value': 1,
              'false-value': 0
            },
            on: {
              'on-change': () => {
                let status = parseInt(params.row.status) ? 0 : 1
                this.changeGoodStatus(params.row.id, status)
              }
            }
          }, [
            h('span', {
              slot: 'open',
              domProps: {
                innerHTML: '上架'
              }
            }),
            h('span', {
              slot: 'close',
              domProps: {
                innerHTML: '下架'
              }
            })
          ]
          )
        },
        {
          key: 'created_time',
          title: '创建时间',
          align: 'center',
          render: (h, params) => {
            return h('strong', params.row.created_time)
          },
        },
        {
          title: '操作',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  style: {
                    marginBottom: '5px',
                    marginRight: '5px',
                    border: 0,
                    background: 'transparent',
                    fontSize: '14px',
                    color: '#377EEF'
                  },
                  on: {
                    click: () => {
                      this.editGood(params.row)
                    },
                  },
                },
                '编辑',
              ),
              h(
                'Poptip',
                {
                  props: {
                    confirm: true,
                    title: '您确定要删除该商品吗?',
                    transfer: true,
                  },
                  on: {
                    'on-ok': () => {
                      this.changeGoodStatus(params.row.id, -1)
                    },
                  },
                },
                [
                  h(
                    'Button',
                    {
                      style: {
                        margin: '0 5px 5px 0',
                        border: 0,
                        background: 'transparent',
                        fontSize: '14px',
                        color: '#377EEF'
                      },
                      props: {
                        placement: 'top',
                      }
                    },
                    '删除',
                  ),
                ],
              ),
            ])
          },
        },
      ],
      list: [],
      cate_list: [],
      price_list: [],
      loading: true,
      filter: {
        page: 0,
        page_size: 20,
        name: '',
        cate_id: 0,
        status: '-2',
        created_range: '',
      },
      created_at: ['', ''],
    }
  },
  watch: {
    'created_at': function () {
      let created_at_start = Time.dateToTimestamp(this.created_at[0])
      let created_at_end = Time.dateToTimestamp(this.created_at[1])
      if (created_at_start !== 0 && created_at_end !== 0) {
        this.filter.created_range = created_at_start + '-' + created_at_end
      } else {
        this.filter.created_range = ''
      }
    }
  },
  methods: {
    change: function (page) {
      this.loading = true
      this.filter.page = page - 1
      this.init()
    },
    search: function () {
      this.filter.page = 0
      this.init()
    },
    addGood: function () {
      let routeData = this.$router.resolve({ name: 'good-edit', params: { id: 0 } });
      window.open(routeData.href)
    },
    editGood: function (obj) {
      let routeData = this.$router.resolve({ name: 'good-edit', params: { id: obj.id } });
      window.open(routeData.href)
    },
    changeGoodStatus (id, status) {
      let _this = this
      let obj = {}
      obj.id = id
      obj.status = status
      changeGoodStatus(obj).then(res => {
        if (res.code === 0) {
          this.$Message.success('操作成功')
          _this.init()
        } else {
          this.$Message.success(res.text)
        }
      })
    },
    init () {
      goodList(this.filter).then(res => {
        if (res.code == 0) {
          this.list = res.data
          this.loading = false
          this.pagenation.currentPage = parseInt(res.currentPage) + 1
          this.pagenation.totalPage = parseInt(res.totalPage)
          this.pagenation.perPage = parseInt(res.perPage)
        }
      })
    },
    formatJson (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    exportData () {
      let _this = this
      _this.filter.page = 0
      _this.filter.page_size = 500
      _this.export_list = []
      _this.export()
    },
    export () {
      let _this = this
      goodList(this.filter).then(res => {
        if (res.code == 0) {
          if (res.data.length) {
            _this.export_list.push.apply(_this.export_list, res.data)
            _this.filter.page++
            _this.export()
          } else {
            _this.filter.page_size = 20
            const { export_json_to_excel } = require('../../excel/Export2Excel');
            const tHeader = ['商品名称', '商品类别', '商品价格', '商品库存', '商品简介', '状态', '创建时间'];
            const filterVal = ['name', 'cate_name', 'price', 'store', 'desc', 'status_name', 'created_time'];
            const data = _this.formatJson(filterVal, _this.export_list)
            export_json_to_excel(tHeader, data, '商品信息')
          }
        }
      })
    },
  },
  mounted () {
    this.init()
    goodCateForSelect().then(res => {
      if (res.code == 0) {
        this.cate_list = res.data
      }
    })
  }
}
</script>
<style>
.edui-notadd .edui-dialog {
	z-index: 2005 !important;
}
.edui-notadd {
	z-index: 2002 !important;
}
</style>
<style scoped lang="less">
/deep/ .edui-notadd .edui-dialog {
	z-index: 5000 !important;
}
/deep/ input::-webkit-input-placeholder {
	color: rgba(153, 153, 153, 0.47) !important;
	font-size: 14px !important;
}
/deep/ input::-moz-placeholder {
	color: rgba(153, 153, 153, 0.47) !important;
	font-size: 14px !important;
}
/deep/ input:-moz-placeholder {
	color: rgba(153, 153, 153, 0.47) !important;
	font-size: 14px !important;
}
/deep/ input:-ms-input-placeholder {
	color: rgba(153, 153, 153, 0.47) !important;
	font-size: 14px !important;
}
</style>
